package mirkofrancuski.battleship.gui;

import mirkofrancuski.battleship.R;
import mirkofrancuski.battleship.game.Board.Direction;
import mirkofrancuski.battleship.game.Cell;
import mirkofrancuski.battleship.game.Coordinates;
import mirkofrancuski.battleship.game.Ship;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;

public class DrawableShip {

	public enum DrawableState {
		NORMAL, SUNKEN, PLACEABLE, UNPLACEABLE
	};
	
	public Ship ship;

	private GameBoard gameBoard;
	public DrawableState state;
	
	private Matrix translate, backup;
	private Bitmap mBitmap, mBitmapSunken, mBitmapPlaceable,
			mBitmapUnPlaceable;

	public DrawableShip(GameBoard gameBoard, Cell shipCell) {
		ship = new Ship(shipCell);
		translate = new Matrix();
		backup = new Matrix();
		
		this.gameBoard = gameBoard;
		
		state = DrawableState.NORMAL;
		
		// setBitmaps(ship);
	}		

	public Matrix getTranslate() {
		return translate;
	}

	public void setBitmaps(Resources res, int cellSize) {
		int height = cellSize;
		int width = height * ship.shipCell.getSize();
		switch (ship.shipCell) {
		case AIRCRAFT_CARRIER:
			mBitmap = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship5), width,
					height, true);
			mBitmapPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship5_placeable), width,
					height, true);
			mBitmapUnPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship5_unplaceable), width,
					height, true);
			mBitmapSunken = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship5_sunken),
					width, height, true);

			break;
		case BATTLESHIP:
			mBitmap = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship4), width,
					height, true);
			mBitmapPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship4_placeable), width,
					height, true);
			mBitmapUnPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship4_unplaceable), width,
					height, true);
			mBitmapSunken = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship4_sunken),
					width, height, true);
			break;

		case SUBMARINE:
			mBitmap = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship3), width,
					height, true);
			mBitmapPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship3_placeable), width,
					height, true);
			mBitmapUnPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship3_unplaceable), width,
					height, true);
			mBitmapSunken = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship3_sunken),
					width, height, true);
			break;
		case CRUISER:
			mBitmap = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship3), width,
					height, true);
			mBitmapPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship3_placeable), width,
					height, true);
			mBitmapUnPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship3_unplaceable), width,
					height, true);
			mBitmapSunken = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship3_sunken),
					width, height, true);
			break;
		case DESTROYER:
			mBitmap = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship2), width,
					height, true);
			mBitmapPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship2_placeable), width,
					height, true);
			mBitmapUnPlaceable = Bitmap.createScaledBitmap(BitmapFactory
					.decodeResource(res, R.drawable.ship2_unplaceable), width,
					height, true);
			mBitmapSunken = Bitmap.createScaledBitmap(
					BitmapFactory.decodeResource(res, R.drawable.ship2_sunken),
					width, height, true);
			break;
		default:
			break;
		}
	}

	public void setState(DrawableState state) {
		this.state = state;
	}

	public Bitmap getBitmap() {
		switch (state) {
		case NORMAL:
			return mBitmap;
		case SUNKEN:
			return mBitmapSunken;
		case PLACEABLE:
			return mBitmapPlaceable;
		case UNPLACEABLE:
			return mBitmapUnPlaceable;
		default:
			break;
		}
		return null;
	}

	public void place() {				
		int dx = gameBoard.calcX(ship.start.vertical);
		int dy = gameBoard.calcY(ship.start.horizontal);
		translate.reset();
		translate.setTranslate(dx, dy);
	}

	public void setBegining(Coordinates begining) {
		ship.start = new Coordinates(begining);
	}

	public void onMove(float dx, float dy) {
		translate.postTranslate(dx, dy);
		gameBoard.invalidate();
	}

	public void rotate() {
		int rotation = 0; 
		if(ship.direction == Direction.HORIZONTAL){
			rotation = 90;
			ship.direction = Direction.VERTICAL;
		} else {
			rotation = -90;
			ship.direction = Direction.HORIZONTAL;
		}				
		
		translate.reset();
		translate.postRotate(rotation);
		
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),
				mBitmap.getHeight(), translate, true);
		mBitmapPlaceable = Bitmap.createBitmap(mBitmapPlaceable, 0, 0,
				mBitmapPlaceable.getWidth(), mBitmapPlaceable.getHeight(),
				translate, true);
		mBitmapUnPlaceable = Bitmap.createBitmap(mBitmapUnPlaceable, 0, 0,
				mBitmapUnPlaceable.getWidth(), mBitmapUnPlaceable.getHeight(),
				translate, true);
		mBitmapSunken = Bitmap.createBitmap(mBitmapSunken, 0, 0,
				mBitmapSunken.getWidth(), mBitmapSunken.getHeight(), translate,
				true);

	}

	public void draw(Canvas canvas) {
		if(ship.start.horizontal == -1 && ship.start.vertical == -1)
			return;
		
		canvas.drawBitmap(getBitmap(), translate, null);
	}

	public DrawableState getState() {
		return state;
	}

}
